package rsb21424yhc.d1117;



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//集合的遍历
public class Demo02 {
    public static void main(String[] args) {
        List<Person> list =new ArrayList<>();
        list.add(new Person("poi","m"));
        list.add(new Person("poi","m"));
        list.add(new Person("poi","m"));
        list.add(new Person("poi","m"));
        list.add(new Person("poi","m"));
        System.out.println(list);
        //集合的遍历
        for (Person per :list){
            System.out.println(per);
        }
        //lambda 的foreach
        list.forEach(obj->{ System.out.print(obj+"----");});
        //迭代器
        //创建 迭代器对象
        System.out.println("[");
        Iterator<Person> it =list.iterator();
        while (it.hasNext()){
            Person p= it.next();//指针移动
            System.out.print( p.getName()+"------");
        }
        System.out.println("]");
        //传统for
        for (int i=0; i<list.size();i++){
            Person p=list.get(i);
            System.out.println(p.getCountry());
        }

    }
    static class Person{
        private String name;
        private String country;

        public Person(String name, String country) {
            this.name = name;
            this.country = country;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        @Override
        public String toString() {
            return this.name +this.country;
        }
    }
}
